
import { GoogleGenAI, Type } from "@google/genai";
import type { FaqItemType } from '../types';

// This function calls the Gemini API to generate FAQ content.
// IMPORTANT: An API key must be set in the environment variable `process.env.API_KEY`.
// The application code assumes this key is provided by the execution environment.
export const generateFaqContent = async (language: string): Promise<FaqItemType[]> => {
    // A real app would get the key from a secure backend or environment variable.
    // We proceed assuming `process.env.API_KEY` is available.
    if (!process.env.API_KEY) {
        console.error("API_KEY environment variable not set.");
        // Fallback to dummy data if API key is not available
        return getDummyFaqs();
    }

    const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });

    const prompt = `Generate 5 frequently asked questions and their answers for a social media video downloader website called "DaoBT". The questions should be relevant to users wanting to download videos from platforms like Instagram, TikTok, Facebook, etc. The tone should be helpful and straightforward. Please provide the response in ${language}.`;

    const responseSchema = {
        type: Type.ARRAY,
        items: {
            type: Type.OBJECT,
            properties: {
                question: {
                    type: Type.STRING,
                    description: 'The frequently asked question.'
                },
                answer: {
                    type: Type.STRING,
                    description: 'The answer to the question.'
                }
            },
            required: ['question', 'answer']
        }
    };

    try {
        const response = await ai.models.generateContent({
            model: "gemini-2.5-flash",
            contents: prompt,
            config: {
                responseMimeType: "application/json",
                responseSchema: responseSchema,
            },
        });
        
        const jsonText = response.text.trim();
        const faqs = JSON.parse(jsonText) as FaqItemType[];

        if (!faqs || faqs.length === 0) {
            return getDummyFaqs();
        }

        return faqs;

    } catch (error) {
        console.error("Gemini API call failed:", error);
        // In case of API error, return dummy data to not break the UI
        return getDummyFaqs();
    }
};

// Dummy data to be used as a fallback if the API call fails or the key is missing.
const getDummyFaqs = (): FaqItemType[] => {
    return [
        {
            question: "Is DaoBT Downloader free to use?",
            answer: "Yes, our video downloader is completely free to use without any limitations. You can download as many videos as you want."
        },
        {
            question: "Is it legal to download videos?",
            answer: "You should only download videos for which you have permission from the copyright owner. Downloading copyrighted material without permission may be illegal. Our service is intended for personal use, such as downloading your own content or publicly available, non-copyrighted videos."
        },
        {
            question: "What video quality can I download?",
            answer: "Our tool strives to provide the highest quality available for the video you are downloading. This often includes HD, Full HD, and even 4K quality if the original video supports it."
        },
        {
            question: "Do I need to install any software?",
            answer: "No, DaoBT is a fully web-based tool. You do not need to install any software or browser extension. Simply paste the video link on our website to start downloading."
        },
        {
            question: "Which social media platforms are supported?",
            answer: "We support downloading from all major social media platforms, including Instagram, TikTok, Facebook, Twitter, and many more. If you have a link, give it a try!"
        }
    ];
};
